package com.example.pxpermission;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.TextView;

import com.tbruyelle.rxpermissions3.RxPermissions;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import io.reactivex.rxjava3.functions.Consumer;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "Main";
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //涉及危险权限请求授权
        RxPermissions rxPermissions = new RxPermissions(this);
        //判断是否同意某个权限
        if (!rxPermissions.isGranted(Manifest.permission.READ_EXTERNAL_STORAGE)) {
            rxPermissions.request(Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE).
                    subscribe(new Consumer<Boolean>() {
                        @Override
                        public void accept(Boolean granted) throws Throwable {
                            //granted 用户是否同意授权权限

                            if (granted) {
                                //授权成功
                                //intiView();
                            }

                        }
                    });
        } else {
            //intiView();
        }
    }
    /*private void intiView() throws IOException {
        //外部存储是否正常挂载
        if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
            //获取外部存储路径的根目录路径
            File rootDir=Environment.getExternalStorageDirectory();
            String rootPath=rootDir.getCanonicalPath();
            //外部存储的路径不是固定的  不同系统版本、不同厂商变化   data/media/0   mnt/sdcard  /sdcard/  模拟器专用/storage/emulated/0
            Log.d(TAG,"外部存储路径:"+rootPath);
           // File extFile=new File(rootPath+"/Download/external_file.txt");

            //读取文件内容
            textView=findViewById(R.id.text_hello);
           FileInputStream fileInputStream=openFileInput("NewTextFile.txt");
            BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(fileInputStream));
            StringBuffer sb=new StringBuffer();
            String str=null;
            while ((str=bufferedReader.readLine())!=null){
                sb.append(str);
            }
            textView.setText(sb.toString());

           // fileList()  getParentDir  isFile

        }*/

    private void intiTextManger() {
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            //获取外部存储路径的根目录路径
            File rootDir = Environment.getExternalStorageDirectory();
            String rootPath = null;
            try {
                rootPath = rootDir.getCanonicalPath();
            } catch (IOException e) {
                e.printStackTrace();
            }
            Log.d(TAG, "外部存储路径:" + rootPath);
            File file=new File(rootPath);
            String name=file.getName();
            file.listFiles();

        }

    }
}